package com.sheng.leetcode.year2023.month07.day31;

import org.junit.Test;

/**
 * @author liusheng
 * @date 2023/07/31
 * <p>
 * 206. 反转链表<p>
 * <p>
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。<p>
 * <p>
 * 示例 1：<p>
 * 输入：head = [1,2,3,4,5]<p>
 * 输出：[5,4,3,2,1]<p>
 * <p>
 * 示例 2：<p>
 * 输入：head = [1,2]<p>
 * 输出：[2,1]<p>
 * <p>
 * 示例 3：<p>
 * 输入：head = []<p>
 * 输出：[]<p>
 * <p>
 * 提示：<p>
 * 链表中节点的数目范围是 [0, 5000]<p>
 * -5000 <= Node.val <= 5000<p>
 * <p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/reverse-linked-list">206. 反转链表</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class LeetCode0206 {

    @Test
    public void test01() {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
//        ListNode head = new ListNode(1, new ListNode(2));
//        ListNode head = new ListNode();
        ListNode listNode = new Solution206().reverseList(head);
        System.out.println(listNode);
    }
}

class Solution206 {
    public ListNode reverseList(ListNode head) {
        ListNode ans = null;
        while (head != null) {
            ans = new ListNode(head.val, ans);
            head = head.next;
        }
        return ans;
    }
}

/*
  Definition for singly-linked list.
  public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
 */
